<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>ADMIN</title>
    <%@include file="common.jsp" %>
    	<!-- BOOTSTRAP STYLES-->
    <link href="../css/bootstrap.css" rel="stylesheet" />
     <!-- FONTAWESOME STYLES-->
    <link href="../css/font-awesome.css" rel="stylesheet" />
        <!-- CUSTOM STYLES-->
    <link href="../css/custom.css" rel="stylesheet" />
     <!-- GOOGLE FONTS-->
   <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
     <!-- CUSTOM SCRIPTS -->
    <script src="../js/custom.js"></script>
</head>
<body>     
    <div id="wrapper">
    	<!-- 头部 -->
         <%@include file="top.jsp" %>
         <!-- 左侧菜单 -->
         <%@include file="menu.jsp" %>
         <!-- 右侧内容 -->
        <div id="page-wrapper">
            <div id="page-inner"style="width:3200px"> 
            	<div class="form-group"style="width:1000px">
	                <%-- <div class="col-sm-4">
	                    <select class="form-control" id="selectUser">
	                        <c:forEach items="${users}" var="user">
	                            <c:if test="${user.userid!='admin'}">
	                                <option value="${user.id}">${user.username}</option>
	                            </c:if>
	                        </c:forEach>
	                    </select>
	                </div> --%>
	                <div class="col-sm-4" >
	                    <div class="col-sm-4 text-center" id="lastMonth">
	                    	<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
	                    </div>
	                    <div class="col-sm-4 text-center" id="currMonth">
	                    </div>
	                    <div class="col-sm-4 text-center" id="nextMonth">
	                    	<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
	                    </div>
	                </div>
				</div>
				<div >
                    <div id="Title">
						<br/>
						<table id="tableId" class="table table-striped table-bordered table-hover" >
							<thead id="theadId">
							</thead>
							<tbody id="content">
							</tbody>
						</table>
                        <!-- <div class="alert alert-danger">
                             <strong>Want More Icons Free ? </strong> Checkout fontawesome website and use any icon <a target="_blank" href="http://fortawesome.github.io/Font-Awesome/icons/">Click Here</a>.
                        </div> -->
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- 底部 -->
<%@include file="footer.jsp" %>
</body>
<script type="text/javascript">

	//当前事件
	var date = new Date();
	$(function () {
	    //init
	    init(date.getFullYear(), date.getMonth());
	    $("#lastMonth").click(function () {
	        $("#tableId").remove();
	        var last = Number($("#currMonth").text().substring(0, 1)) - 1-1;
	        init(date.getFullYear(), last);
	    });
	
	    $("#nextMonth").click(function () {
	        $("#tableId").remove();
	        var next = Number($("#currMonth").text().substring(0, 1));
	        init(date.getFullYear(), next);
	    });
	});
	function init(year, month) {
		//每月天数数组
        var m_days = new Array(31, 28 + isLeapYear(year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        //当月天数
        var currMonthDay = m_days[month];
	    $("#currMonth").text(month + 1 + "月份");
	    $("#theadId").append("<tr id=\"thId\"></tr>");
		$("#thId").append("<th style=\"width:10px\" id=\"thId\">姓名\\日期</th>");
		$("#thId").append("<th style=\"width:20px\">1</th>");
		$("#thId").append("<th style=\"width:20px\">2</th>");
		$("#thId").append("<th style=\"width:20px\">3</th>");
		$("#thId").append("<th style=\"width:20px\">4</th>");
		$("#thId").append("<th style=\"width:20px\">5</th>");
		$("#thId").append("<th style=\"width:20px\">6</th>");
		$("#thId").append("<th style=\"width:20px\">7</th>");
		$("#thId").append("<th style=\"width:20px\">8</th>");
		$("#thId").append("<th style=\"width:20px\">9</th>");
		$("#thId").append("<th style=\"width:20px\">10</th>");
		$("#thId").append("<th style=\"width:20px\">11</th>");
		$("#thId").append("<th style=\"width:20px\">12</th>");
		$("#thId").append("<th style=\"width:20px\">13</th>");
		$("#thId").append("<th style=\"width:20px\">14</th>");
		$("#thId").append("<th style=\"width:20px\">15</th>");
		$("#thId").append("<th style=\"width:20px\">16</th>");
		$("#thId").append("<th style=\"width:20px\">17</th>");
		$("#thId").append("<th style=\"width:20px\">18</th>");
		$("#thId").append("<th style=\"width:20px\">19</th>");
		$("#thId").append("<th style=\"width:20px\">20</th>");
		$("#thId").append("<th style=\"width:20px\">21</th>");
		$("#thId").append("<th style=\"width:20px\">22</th>");
		$("#thId").append("<th style=\"width:20px\">23</th>");
		$("#thId").append("<th style=\"width:20px\">24</th>");
		$("#thId").append("<th style=\"width:20px\">25</th>");
		$("#thId").append("<th style=\"width:20px\">26</th>");
		$("#thId").append("<th style=\"width:20px\">27</th>");
	    //判断是否是1、3、5、7、8、10、11月份
	    if(checkMonth(month)==31){
	    	$("#thId").append("<th style=\"width:20px\">28</th>");
	    	$("#thId").append("<th style=\"width:20px\">29</th>");
	    	$("#thId").append("<th style=\"width:20px\">30</th>");
	    	$("#thId").append("<th style=\"width:20px\">31</th>");
	    }else if(checkMonth(month)==30){
	    	$("#thId").append("<th style=\"width:20px\">28</th>");
	    	$("#thId").append("<th style=\"width:20px\">29</th>");
	    	$("#thId").append("<th style=\"width:20px\">30</th>");
	    }else{
	    	if(checkMonth(month)==28){
	    		$("#thId").append("<th style=\"width:20px\">28</th>");
	    	}
	    }
	    $.ajax({
            url: "getTasksByMonth?month=" + (month + 1)+"&year="+year,
            type: "get",
            success: function (result) {
            	var userList=result.userList;
            	var taskList=result.taskList;
                for (var i = 0; i < userList.length; i++) {
                	var username=userList[i].userid
                	var name=userList[i].username
                	var userid=userList[i].id
                	if(username!="admin"){
                		$("#content").append("<tr id=\"contentThId"+i+"\"></tr>");
                		$("#contentThId"+i).append("<th style=\"width:20px\">"+name+"</th>");
                		var monthDayCount=checkMonth(month)
                        for (var k = 1; k <= monthDayCount; k++) {
                        	var countK=0;
                        	var taskname="";
	                		for (var y = 0; y < taskList.length; y++) {
	                			var task = taskList[y];
	                			var task_userid = taskList[y].userid;
	                			var countStr="";
	                			if(userid==task_userid){
	                				var scheduleStartTime = new Date(task.scheduleStartTime.replace(/-/g, "/"));
	                				//时间小于这月1号，则等于1号
	                				if (scheduleStartTime < new Date(year, month, 1)) scheduleStartTime = new Date(year, month, 1);
	                				var scheduleEndTime = new Date(task.scheduleEndTime.replace(/-/g, "/"));
	                				//时间大于月底，则等于月底
	                				if (scheduleStartTime > new Date(year, month, currMonthDay)) scheduleStartTime = new Date(year, month, currMonthDay);
	                				var isOverTime = (scheduleEndTime - date) < 0 ? true : false;
	                                var timeDiff = scheduleEndTime - scheduleStartTime;
	                                timeDiff = Math.floor(timeDiff / (24 * 3600 * 1000));//时间差，天数
	                                for (var j = 0; j <= timeDiff; j++) {
										var taskFirstDay = scheduleStartTime.getDate()+j;
	                                    if(k==taskFirstDay){
	                                    	//已经结束的任务
		                                	if (task.isEnd == 1) {
		                                		taskname+="<span style=\"color:#DDDDDD\">";
		                                		countK++;
		                                    	countStr=countK+"";
		                                    	taskname+=countStr;
		                                    	taskname+="、";
		                                    	taskname+=task.name;
		                                    	taskname+=";";
		                                    	taskname+="</span>";
		                                    	taskname+="</br>";
		                                 	//逾期的任务
		                                	} else if (isOverTime) {
		                                		taskname+="<span style=\"color:#B22222\">";
		                                		countK++;
		                                    	countStr=countK+"";
		                                    	taskname+=countStr;
		                                    	taskname+="、";
		                                    	taskname+=task.name;
		                                    	taskname+=";";
		                                    	taskname+="</span>";
		                                    	taskname+="</br>";
		                                        //未结束的任务
		                                 	} else {
		                                 		taskname+="<span style=\"color:#444444\">";
		                                 		countK++;
		                                    	countStr=countK+"";
		                                    	taskname+=countStr;
		                                    	taskname+="、";
		                                    	taskname+=task.name;
		                                    	taskname+=";";
		                                    	taskname+="</span>";
		                                    	taskname+="</br>";
		                                 	}
	                                	}
	                                	
	                                }
	                			}
	                		}
	                		if(countK==0){
                            	//alert(username+k+"天没有任务")
                            	$("#contentThId"+i).append("<th style=\"width:20px;color:red;\">暂无安排</th>");
                        	}else{
                        		//alert(username+k+"天有"+countK+"个任务:"+taskname)
                        		$("#contentThId"+i).append("<th style=\"width:20px\">"+taskname+"</th>");
                        	}
                        }
                	}
                }
            }
        });
	}
	function isLeapYear(year) {
		return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);  
	}
	function checkMonth(month){
		if((month + 1)==1 ||(month + 1)==3 ||(month + 1)==5 ||(month + 1)==7 ||(month + 1)==8 ||(month + 1)==10 ||(month + 1)==12){
			return 31;
		}else if((month + 1)==4 ||(month + 1)==6 ||(month + 1)==9 ||(month + 1)==11 ){
			return 30;
	    }else{
	    	if(isLeapYear(year)){
	    		return 28;
	    	}else{
	    		return 27;
	    	}
	    }
	}
</script>
</html>